/*
 * Copyright (c) 2013-2015 Charkey. All rights reserved.
 *
 * This software is the confidential and proprietary information of Charkey.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the agreements you entered into with Charkey.
 *
 * Charkey MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 * Charkey SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package cn.simastudio.talos.ucpaas.model;

import com.alibaba.fastjson.JSON;
import com.jayway.jsonpath.JsonPath;

import java.io.Serializable;
import java.util.Random;

/**
 * Usage:
 *
 * @author Charkey
 * @date 2015/7/2 21:24
 */
public class SMSResponse implements Serializable {

    public static final String SUCCESS_CODE = "000000";

    private static final long serialVersionUID = -7878337651211558887L;

    /**
     * 请求状态码，取值 000000（成功）
     */
    private String respCode;

    /**
     * 表示短信验证码发送失败的条数。注：批量发送时，才会返回该字段
     */
    private String failure;

    private TemplateResp templateSMS;

    private static final SMSResponse mockResponseInstance;

    private static final int mockCaptchaLength = 6;

    private static final String numeral10 = "0123456789";

    private static final Random random = new Random();

    static {
        mockResponseInstance = new SMSResponse();
        StringBuilder sb = new StringBuilder(mockCaptchaLength);
        for (int i = 0; i < mockCaptchaLength; i++) {
            sb.append(numeral10.charAt(random.nextInt(numeral10.length())));
        }
        mockResponseInstance.setRespCode(SUCCESS_CODE);
    }

    /**
     * <p>入参JSON样例如下，这是UCPaaS接口返回的JSON</p>
     * <p>本方法会读取resp节点的数据，转为SMSResponse对象</p>
     * <pre>
     *  {
     *      "resp": {
     *          "respCode": "000000",
     *          "failure": 1,
     *          "templateSMS": {
     *              "createDate": 20140623185016,
     *              "smsId": "f96f79240e372587e9284cd580d8f953"
     *          }
     *      }
     *  }
     * </pre>
     *
     * @param json json
     * @return SMSResponse
     */
    public static SMSResponse getInstanceFromRespJson(String json) {
        Object smsResponseObject = JSON.toJSON(JsonPath.read(json, "$.resp"));
        return JSON.parseObject(smsResponseObject.toString(), SMSResponse.class);
    }

    public static SMSResponse getMockResponse() {
        return mockResponseInstance;
    }

    public String getRespCode() {
        return respCode;
    }

    public void setRespCode(String respCode) {
        this.respCode = respCode;
    }

    public String getFailure() {
        return failure;
    }

    public void setFailure(String failure) {
        this.failure = failure;
    }

    public TemplateResp getTemplateSMS() {
        return templateSMS;
    }

    public void setTemplateSMS(TemplateResp templateSMS) {
        this.templateSMS = templateSMS;
    }

    @Override
    public String toString() {
        return "SMSResponse{" +
                "respCode='" + respCode + '\'' +
                ", failure='" + failure + '\'' +
                ", templateSMS=" + templateSMS +
                '}';
    }
}
